package com.school.controller;

import com.alibaba.fastjson.JSONObject;
import com.school.entity.RealisticRecord;
import com.school.service.RealisticRecordService;
import com.school.untils.ResultEntity;
import com.school.untils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Api(tags = "写实记录管理")
@RestController
@RequestMapping("/realisticRecord")
public class RealisticRecordController {
    @Autowired
    RealisticRecordService realisticRecordService;

    // 增加
    @ApiOperation("写实记录信息增加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultEntity addRealisticRecord(HttpServletRequest servletRequest) throws IOException {
        List<MultipartFile> fileList = null;
        if (servletRequest instanceof StandardMultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
            fileList = multipartRequest.getFiles("file");
        }
        JSONObject userJsonObject = (JSONObject) JSONObject.parse(servletRequest.getParameter("realisticRecord"));
        RealisticRecord realisticRecord = JSONObject.toJavaObject(userJsonObject, RealisticRecord.class);
        return  ResultUtils.build(realisticRecordService.addRealisticRecord(fileList,realisticRecord));
    }

    // 查询所有
    @ApiOperation("写实记录信息查询所有")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public ResultEntity<Object> findAll(RealisticRecord realisticRecord){
        return ResultUtils.build(realisticRecordService.findAll(realisticRecord));
    }

    // 更新
    @ApiOperation("写实记录信息更新")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultEntity updateRealisticRecord(@RequestBody RealisticRecord realisticRecord){
        return ResultUtils.build(realisticRecordService.updateRealisticRecord(realisticRecord));
    }

    // 根据id查看详情
    @ApiOperation("写实记录信息查看详情")
    @GetMapping("/getDetailsById/{id}")
    public ResultEntity getDetailsById(@PathVariable("id") String id){
        return ResultUtils.build(realisticRecordService.getDetailsById(id));
    }

    // 根据id删除
    @ApiOperation("写实记录信息删除")
    @DeleteMapping("/delete/{id}")
    public void deleteRealisticRecord(@PathVariable("id") String id){
        realisticRecordService.deleteRealisticRecord(id);
    }

    // 根据id批量删除
    @ApiOperation("写实记录信息批量删除")
    @DeleteMapping("/batch")
    public void deleteRealisticRecordList(String idList){
        realisticRecordService.deleteRealisticRecordList(idList);
    }

    // 根据条件分页查询
    @ApiOperation("根据条件分页查询")
    @GetMapping("/specificPage")
    public ResultEntity specificPageQueryRealisticRecordList(RealisticRecord realisticRecord,@RequestParam int page,@RequestParam int size){
        return ResultUtils.build(realisticRecordService.specificPageQueryRealisticRecordList(realisticRecord,page,size));
    }

    // 审核状态变更
    @ApiOperation("写实记录信息审核状态变更")
    @RequestMapping(value = "/updateAudit",method = RequestMethod.POST)
    public void updateAudit(@RequestParam("id") String id,@RequestParam("audit") int audit){
        realisticRecordService.updateAudit(id,audit);
    }

    // 审核状态批量变更
    @ApiOperation("写实记录信息审核状态批量变更")
    @RequestMapping(value = "/updateAuditList",method = RequestMethod.POST)
    public void updateAudit(@RequestParam("idList") List<String> idList,int auditList){
        realisticRecordService.updateAuditList(idList,auditList);
    }

    // 查询不同状态的写实记录信息
    @ApiOperation("写实记录信息数据不同状态查询")
    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    public ResultEntity findListByAuditStatus(@RequestParam("audit") int audit){
        return ResultUtils.build(realisticRecordService.findListByAuditStatus(audit));
    }

    // 查询除了待确认以外的写实记录信息
    @ApiOperation("写实记录信息数据待确认以外查询")
    @RequestMapping(value = "/findExceptList",method = RequestMethod.GET)
    public ResultEntity findExceptList(@RequestParam int page,@RequestParam int size){
        return ResultUtils.build(realisticRecordService.findExceptList(page,size));
    }

    // 公示展示
    @ApiOperation("写实记录信息数据公示展示")
    @RequestMapping(value = "/findDisplayList",method = RequestMethod.GET)
    public ResultEntity findDisplayList(RealisticRecord realisticRecord,@RequestParam int page,@RequestParam int size){
        return ResultUtils.build(realisticRecordService.findDisplayList(realisticRecord,page,size));
    }

    // 事实材料上传
    @ApiOperation(value = "事实材料上传(file:文件，path:文件路径)", notes = "事实材料上传")
    @RequestMapping(value = "/updateFile",method = RequestMethod.POST)
    public ResultEntity updateFile(HttpServletRequest servletRequest) throws IOException {
        List<MultipartFile> fileList = null;
        if (servletRequest instanceof StandardMultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
            fileList = multipartRequest.getFiles("file");
        }
        String path = servletRequest.getParameter("path");
        return ResultUtils.build(realisticRecordService.updateFile(fileList,path));
    }

    // 老师驳回原因更新
    @ApiOperation("写实记录驳回原因信息更新")
    @RequestMapping(value = "/updateRejectionReason",method = RequestMethod.POST)
    public ResultEntity updateRejectionReason(@RequestParam("id") String id,@RequestParam("rejectionReason") String rejectionReason){
        return ResultUtils.build(realisticRecordService.updateRejectionReason(id,rejectionReason));
    }

    // 根据学校ID和班级ID查询所有
    @ApiOperation("写实记录信息根据学校ID和班级ID查询所有")
    @RequestMapping(value = "/findAllBySchoolIdAndClassId",method = RequestMethod.GET)
    public ResultEntity<Object> findAllBySchoolIdAndClassId(Integer schoolId,Integer classId){
        return ResultUtils.build(realisticRecordService.findAllBySchoolIdAndClassId(schoolId,classId));
    }
}
